package com.xcity.tools.skill;

import java.io.File;
import java.text.MessageFormat;

import com.xcity.game.skill.template.SkillTemplate;

import naga.x.App;
import naga.x.common.Config;
import naga.x.game.template.TemplateParser;
import naga.x.game.template.TemplateService;
import naga.x.util.IOUtils;

public class PassiveSkillEffectorGenerator {

	static File baseDir = new File("F:/prj_java_svn/city-server/src/main/java/com/xcity/game/skill/v2");
	
	enum Group {
		G_ATK("攻击强化"),
		G_DEF("防御强化"),
		G_HP("生命强化"),
		G_CRIT("暴击强化"),
		G_AVOID_CRIT("韧性强化"),
		;
		final String desc;
		Group(String name) {
			this.desc = name;
		}
	}
	
	static class Def {
		Group group;
		int[] ids;
		Def(Group group, int... ids) {
			this.group = group;
			this.ids = ids;
		}
	}
	
	static Def[] defs = new Def[] {
			new Def(Group.G_ATK, 100101,
							100102,
							100103,
							100104),
			new Def(Group.G_DEF, 100201,
							100202,
							100203,
							100204),
			new Def(Group.G_HP, 100301,
							100302,
							100303,
							100304),
			new Def(Group.G_CRIT, 100401),
			new Def(Group.G_AVOID_CRIT, 100501),
	};
	
	
	public static void main(String[] args) throws Exception {
		App.getApp().setConfig(new Config());
//		App.getApp().getConfig().put(Config.DATA_DIR, "C:/Users/yang.li/Desktop/server_release/data");
		App.getApp().getConfig().dataDir = "C:/Users/yang.li/Desktop/server_release/data";
		
		TemplateService service = new TemplateService();
		TemplateParser parser = new TemplateParser(service);
		parser.parse(SkillTemplate.class);
		
		StringBuilder builder = new StringBuilder(1024);
		String codeTemplate = IOUtils.read(IOUtils.getResource("template_passive_skill.txt"), IOUtils.UTF8);
		for (Def def : defs) {
			System.out.println(def.group.desc);
			
			String syntax1 = "#segbegin#";
			int from = codeTemplate.indexOf(syntax1);
			String syntax2 = "#segend#";
			int to = codeTemplate.indexOf(syntax2, from + syntax1.length());
			
			String seg = codeTemplate.substring(from + syntax1.length(), to);
			String prefix = codeTemplate.substring(0, from);
			String suffix = codeTemplate.substring(to + syntax2.length());
			
			syntax1 = '#' + def.group.name() + '#';
			from = seg.indexOf(syntax1);
			syntax2 = "#end#";
			to = seg.indexOf(syntax2, from + syntax1.length());
			String var = seg.substring(from + syntax1.length(), to);
			
			
			prefix = prefix.replace("#group#", def.group.name())
							.replace("#desc#", def.group.desc);
			builder.append(prefix);
			builder.append(var);
			builder.append(suffix);
			
			File file = new File(baseDir, "impl/PASSIVE_SKILL_" + def.group + ".java");
			IOUtils.write(file, builder.toString(), IOUtils.UTF8);
			builder.setLength(0);
		}
		
		String codeFactory = IOUtils.read(IOUtils.getResource("template_passive_skill_factory.txt"), IOUtils.UTF8);
		int from = codeFactory.indexOf("#foreach#");
		int to = codeFactory.indexOf("#end#", from);
		StringBuilder buf = new StringBuilder(1024);
		String prefix = codeFactory.substring(0, from);
		buf.append(prefix); // prefix
		String foreach = codeFactory.substring(from + "#foreach#".length(), to);
		for (Def def : defs) {
			for (int id : def.ids) {
				SkillTemplate template = service.get(SkillTemplate.class, id);
				String code = foreach.replace("#id#", String.valueOf(id))
									.replace("#name#", template.getName())
									.replace("#desc#", MessageFormat.format(template.getDesc(), template.getInitVals()))
									.replace("#group#", def.group.name());
				buf.append(code);
			}
		}
		String suffix = codeFactory.substring(to +  + "#end#".length());
		buf.append(suffix); // suffix
		IOUtils.write(new File(baseDir, "SkillEffectorFactory.java"), buf.toString(), IOUtils.UTF8);
		System.out.println("ok");
	}
	
}
